﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.XPath;
using System.Net;

namespace Real_Estate.Code.OBJ
{
    public class Googlegeocode
    {
        //Declarando a variavel que irá guardar a url da busca 
        string URL = "http://maps.googleapis.com/maps/api/geocode/xml?adress=";
        private double Lat;
        public double lat
        {
            get { return Lat; }
            set { Lat = value; }
        }

        private double Lng;
        public double lng
        {
            get { return Lng; }
            set { Lng = value; }
        }


        //Construtor
        public Googlegeocode(string Local)
        {
            URL += HttpUtility.UrlEncode(Local);
            URL += "&sensor=false";
        }

        //Realizando busca
        public string CalculaCoordenadas()
        {
            //Realiza a requisição ao webservice do google
            WebResponse Resposta = null;
            try
            {
                HttpWebRequest Requisicao = (HttpWebRequest)WebRequest.Create(URL);
                Requisicao.Method = "GET";
                Resposta = Requisicao.GetResponse();

                //Uma vez adquirida uma resposta verificar no cabeçalho o status dela
                if (Resposta != null)
                {
                    XPathDocument Documento = new XPathDocument(Resposta.GetResponseStream());
                    XPathNavigator Navegador = Documento.CreateNavigator();
                    XPathNodeIterator StatusIterator = Navegador.Select("/geocoderesponse/status");
                    while (StatusIterator.MoveNext())
                    {
                        if (StatusIterator.Current.Value != "OK")
                        {
                            return "Erro" + StatusIterator.Current.Value;
                        }


                        //Agora iremos ler a resposta e obter as coordenadas

                        XPathNodeIterator ResponseIterator = Navegador.Select("/geocoderesponse/response");
                        while (ResponseIterator.MoveNext())
                        {
                            XPathNodeIterator latIterator = ResponseIterator.Current.Select("lat");
                            while (latIterator.MoveNext())
                            {
                                lat = Convert.ToDouble(latIterator.Current.Value);
                            }

                            XPathNodeIterator lngIterator = ResponseIterator.Current.Select("lng");
                            while (lngIterator.MoveNext())
                            {
                                lng = Convert.ToDouble(lngIterator.Current.Value);
                            }
                        }

                    }

                }

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                //Fechar resposta
                Resposta.Close();
                Resposta = null;
            }
            return "OK";
        }

        //Retornando os valores

        public float RetornaLat()
        {
             return (float)lat;
        }

        public float RetornaLng()
        {
            return (float)lng;
        }
    }
}